<?php
/**
 * Created by PhpStorm.
 * User: seven
 * Date: 14-12-18
 * Time: 下午4:55
 */

require 'global.php';
require "FeyinApi.class.php";

$feyin = new FeiyinAPI();

$ref_no = '201412220957372942';
$order_detail = order2detail($ref_no);
print_r($order_detail);
/*mode=1*/
/*$orderId = time() + 1;              //订单编号
$total_price = 3000;                //订单总价
$customerName = '刘小姐';            //客户姓名
$customerPhone = '13321332245';     //联系电话
$customerAddress = '五山华南理工';    //联系地址
$customerMemo = '请快点送货';         //备注
$msgDetail = '番茄炒粉@1000@1||客家咸香鸡@1000@2';   //订单详情：产品名称@产品价格@产品数量||产品名称@产品价格@产品数量*/
$orderId = time() + 1;
$total_price = $order_detail['total_price'];
$customerName = $order_detail['customerName'];
$customerPhone = $order_detail['customerPhone'];
$customerAddress = $order_detail['customerAddress'];    //联系地址
$customerMemo = $order_detail['customerMemo'];         //备注
$msgDetail = $order_detail['msgDetail'];   //订单详情：产品名称@产品价格@产品数量||产品名称@产品价格@产品数量

/*$feyin->setMsgNo($orderId);
$feyin->setCharge("$total_price");
$feyin->setParameter('customerName', "$customerName");
$feyin->setParameter('customerPhone', "$customerPhone");
$feyin->setParameter('customerAddress', "$customerAddress");
$feyin->setParameter('customerMemo', "$customerMemo");
$feyin->setParameter('msgDetail', "$msgDetail");*/


/*mode=2*/
$str = tryOrder($ref_no);
$feyin->setMsgNo(time()+1);
/*$feyin->setParameter('msgDetail', '
    北京订餐网欢迎您订购


条目         单价（元）    数量
------------------------------
番茄炒粉
             10.0          1
客家咸香鸡
             20.0          1

备注：请快点送货。
------------------------------
合计：30.0元

送货地址：北京中关村科技大厦23楼
联系电话：1380017****
订购时间：2011-01-06 19:30:10
');*/
$feyin->setParameter('msgDetail', $str);

pre($str);
$demo = $feyin->formatParatostring();
echo $feyin_status =  $feyin->sendMessage();

/*$feyin_status 返回状态
 *
 * 0 ：正常

-1 ：IP地址不允许

-2 ：关键参数为空或请求方式不对

-3 ：客户编码不对

-4 ：安全校验码不正确

-5 ：请求时间失效

-6 ：订单内容格式不对

-7 ：重复的消息 （ msgNo 的值重复）

-8 ：消息模式不对

-9 ：服务器错误

-10 ：服务器内部错误

-111 ：打印终端不属于该账户
 *
 *
 */

function replace32tostr($str = ' ', $str_old = '', $align = 'l', $w = 32){
    $len = mb_strlen($str, 'utf-8');
    $re_len = 0;
    $re_len_old = 0;

    $len_old = mb_strlen($str_old, 'utf-8');
    for($i = 0; $i < $len_old; $i++){
        if(strlen(mb_substr($str_old, $i, 1, 'utf-8')) == 3){
            $re_len_old += 2;
        }else{
            $re_len_old += 1;
        }
    }


    for($i = 0; $i < $len; $i++){
        if(strlen(mb_substr($str, $i, 1, 'utf-8')) == 3){
            $re_len += 2;
        }
    }
    $re_len = $w + $re_len / 2;

    $re_old_s = 32 - $re_len_old % 32;
    //echo $re_old_s.'<br>';
    if($re_old_s < $w && $re_old_s != 0){
        //for($i = 0; $i < $re_old_s; $i++){
        //$str_old .= '
//';
        //}
    }

    $strn = '';
    if($align == 'c'){
        $strn = str_pad($str, $re_len, ' ', STR_PAD_BOTH);
    }
    if($align == 'r'){
        $strn = str_pad($str, $re_len, ' ', STR_PAD_LEFT);
    }
    if($align == 'l'){
        $strn = str_pad($str, $re_len, ' ', STR_PAD_RIGHT);
    }
    if(strlen($str) >= $re_len){
        //$str .= ' ';
        $strn .= '
';
        for($i = 0; $i < $w + $re_len_old % 32; $i++){
            $strn .= ' ';
        }

    }
    $str = $str_old.$strn;
    return $str;
}
/*function replace32tostr($str = ' ', $str_old = '', $align = 'l', $w = 32){
    $len = mb_strlen($str, 'utf-8');
    $re_len = 0;
    $re_len_old = 0;

    $len_old = mb_strlen($str_old, 'utf-8');
    for($i = 0; $i < $len_old; $i++){
        if(strlen(mb_substr($str_old, $i, 1, 'utf-8')) == 3){
            $re_len_old += 2;
        }else{
            $re_len_old += 1;
        }
    }

    for($i = 0; $i < $len; $i++){
        if(strlen(mb_substr($str, $i, 1, 'utf-8')) == 3){
            $re_len += 2;
        }
    }
    $re_len = $w + $re_len / 2;
    $strn = '';
    if($align == 'c'){
        $strn = str_pad($str, $re_len, ' ', STR_PAD_BOTH);
    }
    if($align == 'r'){
        $strn = str_pad($str, $re_len, ' ', STR_PAD_LEFT);
    }
    if($align == 'l'){
        $strn = str_pad($str, $re_len, ' ', STR_PAD_RIGHT);
    }
    if(strlen($str) >= $re_len){
        $strn .= '
';
        for($i = 0; $i < $w + $re_len_old % 32; $i++){
            $strn .= ' ';
        }
    }
    return $str_old.$strn;
}*/

function tryOrder($ref_no){

    $order_detail = order2detail($ref_no);
    $companyName = '我家餐饮-欢迎您订购';
    $msgNo = $ref_no;
    $total_price = $order_detail['total_price'];
    $customerName = $order_detail['customerName'];
    $customerPhone = $order_detail['customerPhone'];
    $customerAddress = $order_detail['customerAddress'];    //联系地址
    $customerMemo = $order_detail['customerMemo'];         //备注
    $msgDetail = $order_detail['msgDetail'];   //订单详情：产品名称@产品价格@产品数量||产品名称@产品价格@产品数量
    $str1 = replace32tostr('商品名称', '', 'l', 14);
    $str1 = replace32tostr('单价', $str1, 'l', 6);
    $str1 = replace32tostr('数量', $str1, 'l', 6);
    $str1 = replace32tostr('总计', $str1, 'l', 6);
    $detail = explode('||', $msgDetail);
    pre($detail);
    $str = '';
    $msgDetail = '';
    foreach($detail as $key => $val){
        $val_str = explode('@', $val);
        $str2 = '';
        $str2 = replace32tostr($val_str[0], $str2, 'l', 14);
        $str2 = replace32tostr(($val_str[1] / 100), $str2, 'l', 6);
        $str2 = replace32tostr($val_str[2], $str2, 'l', 6);
        $str2 = replace32tostr(($val_str[1] * $val_str[2] / 100), $str2, 'l', 6);
        $str .= $str2.'
';
    }
    //$msgDetail的代码位置不能改变，必须左对齐
    $msgDetail = replace32tostr($companyName,'', 'c').'

订单编号：'.$msgNo.'
订购时间：'.date('Y-m-d H:i:s').'
客户姓名：'.$customerName.'
联系电话：'.$customerPhone.'
送货地址：'.$customerAddress.'
'.$str1.'
------------------------------
'.$str.'
备注：'.$customerMemo.'
------------------------------
合计：'.($total_price / 100).'元

';
    return $msgDetail;
}

function senOrder($ref_no, $model = 1){
    $feyin = new FeiyinAPI();
    $order_detail = order2detail($ref_no);
    $orderId = $ref_no;                                     //订单编号
    $total_price = $order_detail['total_price'];            //订单总价
    $customerName = $order_detail['customerName'];          //客户姓名
    $customerPhone = $order_detail['customerPhone'];
    $customerAddress = $order_detail['customerAddress'];    //联系地址
    $customerMemo = $order_detail['customerMemo'];         //备注
    $msgDetail = $order_detail['msgDetail'];   //订单详情：产品名称@产品价格@产品数量||产品名称@产品价格@产品数量
}

function order2detail($ref_no){
    global $conn,$sales_order,$product,$product_variation,$arrcurrency,$baseurl,$lang,$option_label,$option_value,$country, $county, $province, $city;
    $order_arr = array();
    $order=$sales_order->get_row($ref_no,'ref_no');
    $sales_order_detail = new DB(Tsales_order_detail, $conn);
    $items=$sales_order_detail->get_rows(1024,'sales_order_id',$order['id']);

    $order_arr['customerName'] = $order['contact_name'];
    $order_arr['customerPhone'] = $order['mobile'];
    $order_arr['total_price'] = $order['order_price'] * 100;
    $tem_province=$province->get_name($order['province'],$lang);
    $tem_addr=$tem_province;
    if(! empty($order['city'])) { $tem_city=$city->get_name($order['city'],$lang); $tem_addr.=$tem_city.'/'; }
    if(! empty($order['county'])) { $tem_county=$county->get_name($order['county'],$lang); $tem_addr.=$tem_county.'/'; }
    $tem_addr.=' '.$order['address'];
    $order_arr['customerAddress'] = $tem_addr;
    $order_arr['customerMemo'] = $order['customer_remark'];

    // $arr_product
    $arr_product=$in_array_product=array();
    foreach($items as $val) $in_array_product[]=$val['product_id'];
    $where=array();
    $where[]=array('name'=>'id','val'=>$in_array_product,'opn'=>'in');
    $tem=$product->get_rows_advanced(1024,$where);
    foreach($tem as $val) $arr_product[$val['id']]=$val;

    // currency
    $order_currency_sign=$arrcurrency[$order['currency_id']]['sign'];
    $order_currency_code=$arrcurrency[$order['currency_id']]['code'];

    $total_qty=0;
    $msgDetail_arr = array();
    foreach ($items as $key => $val){
        $total_qty+=$val['quantity'];
        $tem_product=$arr_product[$val['product_id']];
        $tem_price=$val['unit_price'] * 100;
        $tem_name=str_cut($tem_product['name_'.$lang],PRODUCT_TITLE_DISPLAY_LENGTH,$lang);
        $msgDetail_arr[] = $tem_name.'@'.$tem_price.'@'.$val['quantity'];
    }
    $order_arr['msgDetail'] = implode('||', $msgDetail_arr);
    return $order_arr;
}
